$(function(){
	getMobile();
	//点击获取验证码
    $('.safe-code').click(function(){
    	send();
    	sendVertifyCode();
        return false;
    });
    //点击确定按钮
    $(".submitButton").click(function(){
    	confirmSubmit();
    });
});

/*设置全局变量sendTime */
var sentTime= 59;
/* 发送验证码 */
function send() {
	if(sentTime == 0) {
		$('.safe-code').removeAttr('disabled');
		$('.safe-code').val('发送验证码');
		sentTime=59;
	}else {
		$('.safe-code').attr('disabled',true);
		$('.safe-code').val('重发验证码（'+sentTime+'）');
		$('.safe-code').css("color","#999");
		sentTime--;
		setTimeout(function(){send();},1000);
	}
}
//获取用户手机号
function getMobile() {
	var loginName = getParam("loginName")||'';
	if(!loginName){
        $("#errorText").text("用户不存在").css("color","red");
        $(".errormsg").show();
        return;
	}
	$.ajax({
    	url: "/forget/getUserMobile.do",
    	data: {"loginName":loginName},
    	dataType: "json",
    	success: function (data) {
    		if(data&&data.returnFlag == 'fail'){
                $("#errorText").text(data.msg).css("color","red");
                $(".errormsg").show();
    		}else{
    			$("#mobileId").text(data.mobileEncry)
    			$("#mobile").val(data.mobilePhone);
                $(".errormsg").hide();
    		}
    	}
    });
}
//调用后台短信验证码接口
function sendVertifyCode() {
	var loginName = getParam("loginName")||'';
	var mobile = $("#mobile").val()||'';
	if(!loginName||!mobile){
		$(".sucmsg").hide();
        $("#errorText").text("用户不存在或手机号码有误").css("color","red");
        $(".errormsg").show();
        return;
	}
	$.ajax({
    	url: "/sms/sendVerifyCode.do?num=6&minute=15",
    	data: {"mobile":mobile},
    	dataType: "json",
    	success: function (data) {
    		if(data&&data.returnFlag == "success"){
    			$(".sucmsg").show();
    			$(".errormsg").hide();
    		}else{
    			$(".sucmsg").hide();
    	        $("#errorText").text(data.msg).css("color","red");
    	        $(".errormsg").show();
    		}
    	}
    });
}
//点击确定
function confirmSubmit(){
	var mobileCode = $("#mobileCode").val()||"";
	var mobile = $("#mobile").val()||"";
    if(mobileCode == ""){
        $("#errorText").text("请填写验证码");
        $(".errormsg").show();
        return false;
    }
    if(!mobile){
		$(".sucmsg").hide();
        $("#errorText").text("手机号码有误").css("color","red");
        $(".errormsg").show();
        return;
	}
    //获取上一个页面传递过来的参数
    var loginName = getParam("loginName")||'';
    $.ajax({
        type: "POST",
        url: "/sms/checkMobileCode.do",
        data: {
        	'mobileCode':mobileCode,
        	"mobile":mobile
        } ,
        dataType: "json",
        success: function (data) {
        	if(data&&data.returnFlag == 'fail'){
                $("#errorText").text("验证失败,请重新发送验证短信并填写正确验证码");
                $(".errormsg").show();
                $(".sucmsg").hide();
            }else{
                $("#sucText").text("验证成功");
                $(".errormsg").hide();
                $(".sucmsg").show();
                $('#codeStatus').val(1);
                location.href = 'editEmail.html?'+encryptParam("loginName="+loginName);
            }
        }
     });
}